//
//  TwoViewController.m
//  DemoTest
//
//  Created by DaShuEdu on 2017/4/25.
//  Copyright © 2017年 DaShuEdu. All rights reserved.
//

#import "TwoViewController.h"
#import <WebKit/WebKit.h>

@interface TwoViewController ()

@property (nonatomic, strong) WKWebView *webView;

@end

@implementation TwoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.webView];
}

- (WKWebView *)webView {
    if (_webView == nil) {
        CGRect screenSize = self.view.bounds;
        CGRect frame = CGRectMake(0, 0, screenSize.size.width, screenSize.size.height-49);
        WKWebViewConfiguration *webViewConfig = [[WKWebViewConfiguration alloc] init];
        WKUserContentController *userContentC = [[WKUserContentController alloc] init];
        webViewConfig.userContentController = userContentC;
        WKWebView *webView = [[WKWebView alloc] initWithFrame:frame configuration:webViewConfig];
        NSString *path = [self changeFileDir:@"information_details" type:@"html"];//[[NSBundle mainBundle] pathForResource:@"information_details" ofType:@"html"];
        NSString *base = [[NSBundle mainBundle] bundlePath];
        NSLog(@"path:%@",path);
        
//        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]];
        base = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        base = [path stringByDeletingLastPathComponent];
        [webView loadFileURL:[NSURL fileURLWithPath:path] allowingReadAccessToURL:[NSURL fileURLWithPath:base]];
//        [webView loadRequest:request];
        _webView = webView;
    }
    return _webView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSString *)changeFileDir:(NSString *)name type:(NSString *)type
{
    NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:type inDirectory:@"www"];
    NSString *DocumentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *toPath = [DocumentPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@",name,type]];
    BOOL isSuccess = [[NSFileManager defaultManager] copyItemAtPath:path toPath:toPath error:NULL];
    if (isSuccess) {
        return toPath;
    }
    return path;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
